import 'dart:async';

import 'package:shopping_group/entity/count_entity.dart';
import 'package:shopping_group/res/colors.dart';

class VerificationUtil {
  static void reGetCountdown(CountEntity countEntity, {Function addDataCall}) {
    countEntity.colorStr = Colours.gray_cc;
    countEntity.countdownTimer = Timer.periodic(Duration(seconds: 1), (timer) {
      if (countEntity.countdownNum > 0) {
        countEntity.codeCountdownStr = '已发送（${countEntity.countdownNum--}s）';
        countEntity.colorStr = Colours.gray_cc;
      } else {
        countEntity.colorStr = Colours.themeColor;
        countEntity.codeCountdownStr = '获取验证码';
        countEntity.countdownNum = 59;
        cancelTimer(countEntity);
      }
      if (null == addDataCall) {
        return;
      }
      addDataCall(countEntity);
    });
  }

  static void cancelTimer(CountEntity countEntity) {
    if (countEntity.countdownTimer != null) {
      countEntity.countdownTimer.cancel();
      countEntity.countdownTimer = null;
    }
  }
}
